package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.*;

import utils.UsersInfo;

import models.Location;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionAdoption;
import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxBenefitSearchSolutionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
		try{

		//out.println("<TABLE><TBODY><TR><TD width=100><SPAN class='lotusBtn lotusBtnAction lotusLeft'><A href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/ExportAdoptionMatrixLevel1?openagent'>Export XL</A></SPAN></TD></TR></TBODY></TABLE>");		
		// out.println("<P style='FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 14px; FONT-WEIGHT: bold' width=120px colspan='5'>Savvy Solution Adoption Matrix</P>");
		
		//tempQuery= "customer=" + tempval1 + "&site=" + tempval2 + "&process=" + tempval3 + "&fromDt=" + tempval4 + "&toDt=" + tempval5
		
		String sprocess = (String) req.getParameter("process");
		String slocation = (String) req.getParameter("site");		
		String scustomer = (String) req.getParameter("customer");
		String fromDt = req.getParameter("fromDt");
		String toDt = req.getParameter("toDt");	
		String achievedType = req.getParameter("benefit");	

		Date today = new java.util.Date();
		SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat formatNowMonth = new SimpleDateFormat("MMMMM");
		String tYear = formatNowYear.format( today ); 
		String tMonth = formatNowMonth.format(today );
		
		
		if( sprocess!= null && ( sprocess.equals("0") || sprocess.equals("") ) ){
			sprocess = null;
		}
		if( slocation!=null && ( slocation.equals("0") || slocation.equals("") )){
			slocation = null;
		}
		if( scustomer!=null && ( scustomer.equals("0")|| scustomer.equals("") ) ){
			scustomer = null;
		}
		if( fromDt!=null && ( fromDt.equals("0")|| fromDt.equals("") ) ){
			fromDt = null;
		}
		if( toDt!=null && ( toDt.equals("0")|| toDt.equals("") ) ){
			toDt = null;
		}
		if( achievedType!=null && ( achievedType.equals("0")|| achievedType.equals("") ) ){
			achievedType = null;
		}
		
		Objectify ofy = ObjectifyService.begin();		
		Query<SolutionSubmission> q = null;		
		boolean bProcess, bLocation, bCustomer, bRegion;
		bProcess = bLocation = bCustomer = bRegion = false;
		
		boolean bfromDt = false;
		boolean btoDt = false;
		boolean bachievedType = false;

		if( sprocess != null ){
			bProcess = true;
		}
		if( slocation != null ){
			bLocation = true;
		}
		if( scustomer != null ){
			bCustomer = true;
		}
		if (fromDt != null)
		{
			bfromDt = true;
		}
		if (toDt != null)
		{
			btoDt = true;
		}
		if (achievedType != null)
		{
			bachievedType = true;
		}
		
		out.println("<B>Savvy Solutions Shared by " + achievedType + ":</B>");
		out.println("<TABLE id=insured_list class=tablesorter");
		out.println("<COLGROUP>");
		out.println("<COL style='WIDTH: 60px'>");
		out.println("<COL style='WIDTH: 143px'>");
		out.println("<COL style='WIDTH: 174px'>");
		out.println("<COL style='WIDTH: 82px'>");
		out.println("<COL style='WIDTH: 38px'>");
		out.println("<COL style='WIDTH: 58px'>");
		out.println("<COL style='WIDTH: 60px'></COLGROUP>");
		out.println("<THEAD sizcache='0' sizset='0'>");
		out.println("<TR sizcache='0' sizset='0'>");
		out.println("<TH class=header width=50>Solution ID</TH>");
		out.println("<TH class=header width=100>Title</TH>");
		out.println("<TH class=header width=150>Customer</TH>");
		out.println("<TH class=header width=150>Process</TH>");
		out.println("<TH class=header width=100>Benefit Results</TH>");
		out.println("<TH class=header width=150># Days to Implement</TH>");
		out.println("<TH class=header width=150># Posted Date</TH></TR></THEAD>");
		
		
		q = ofy.query(SolutionSubmission.class).filter("achievedTypeDesc", achievedType);
		if ( bProcess){ q.filter("processArea", sprocess); }		
		if( bLocation){  q.filter("Location", slocation); }
		if( bCustomer){ q.filter("Customer", scustomer); }
		if (bfromDt)
		{
			Date fdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(fromDt + " 00:00:01");
			q.filter("submittedON >=", fdate);
		}
		if (btoDt)
		{
			Date tdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(toDt + " 23:59:59");
			q.filter("submittedON <=", tdate);
		}
		
		out.println("<TBODY>");
		//long recommendCount = 0;
		//String tempStr  = "";
		//int cnt = 0;
		
		for ( SolutionSubmission ff : q.fetch() ) {
			
			//SEARCH THE RECOMMEND	
			//RecommendConnect REC = new RecommendConnect();
			//recommendCount = REC.getCount("solID", ff.getSolID());
			//tempStr = "<a href='#' rel='/modules/default/expert-info-display.jsp?sol_id=" + ff.getSolID() + "' id='sticky" + cnt++ + "' title='Experts Recommended'>";
			
			out.println("<TR class=even>");
			out.println("<TD width=80><A href='/modules/default/display-solution.jsp?sol_id=" + ff.getSolID() + "'>" + ff.getSolCode()  + "</A></TD>");
			out.println("<TD width=100>" + ff.getTitle() + "</TD>");
			out.println("<TD width=150>" + ff.getCustomer() + "</TD>");
			out.println("<TD width=150>" + ff.getProcessArea() + "</TD>");
			out.println("<TD width=300>" + ff.getAchievedTypeDesc() + "</TD>");
			out.println("<TD style='TEXT-ALIGN: center' width=100>" + ff.getImpDays() + "</TD>");
			out.println("<TD width=100>" + ff.getSubmittedON() + "</TD></TR>");
			
			//out.println("<TD style='TEXT-ALIGN: center' width=70>" + tempStr + recommendCount + "<img src='/img/Thumbicon.gif' /></A></TD></TR>");
			//out.println("<TD style='TEXT-ALIGN: center' width=70 sizcache='2' sizset='8' jQuery1347269498656='660'><A id=msticky0 title='Experts Recommended' href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734' rel=/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734 sizcache='2' sizset='8' >0<IMG src='/img/Thumbicon.gif'>" + recommendCount + "</A> </TD></TR>");
			
		}
		out.println("</TBODY></TABLE>");
		
		out.println("<DIV style='DISPLAY: none' id=countera>" + q.countAll() + "</DIV><BR><BR>");
		
		}catch(Exception ee) {}
	}
}
